//
//  SettingInterfaceViewController.m
//  ProjectA
//
//  Created by 李赐岩 姜雪 张昕锐 on 15/3/5.
//  Copyright (c) 2015年 李赐岩 姜雪 张昕锐. All rights reserved.
//

#import "SettingInterfaceViewController.h"

@interface SettingInterfaceViewController ()

@end

@implementation SettingInterfaceViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我  的";
    
    
    //默认登陆状态为未登陆
    self.loginState = NO;
    
    //HEAD IMAGE VIEW
    self.headImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"user-background.jpg"]];
    self.headImageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * 0.43);
    
    
    [self.view addSubview:self.headImageView];
    
    
    //USER IMAGEVIEW
//    self.userImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"userHead.png"]];
//    self.userImageView.frame = CGRectMake(0, 0, self.view.frame.size.width * 0.26 ,  self.view.frame.size.width * 0.26);
//    self.userImageView.center = CGPointMake(self.view.center.x, self.view.center.y * 0.48);
   
    
//添加手势
//    UIGestureRecognizer *gesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//    
//    self.userImageView.userInteractionEnabled = YES;  //开启交互
//    
//    [self.userImageView addGestureRecognizer:gesture1];
//    
//    
//    [self.view addSubview:self.userImageView];
    
    
    //提示语
//    self.loginButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    self.loginButton.frame = CGRectMake(0, self.userImageView.frame.size.height + self.userImageView.frame.origin.y + 10, self.view.frame.size.width, 50);
//    [self.loginButton setTitle:@"点击头像登陆" forState:(UIControlStateNormal)];
//    [self.loginButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
//    
//    [self.view addSubview:self.loginButton];
    
    
//TABLEVIEW
    UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height * 0.48, self.view.frame.size.width, 160) style:(UITableViewStylePlain)];
    
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.scrollEnabled = NO;
    
//    [tableview setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    
    [self.view addSubview:tableview];
    
    
    
} //DID LOAD ***


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}//section number


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}//height


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
    
} //row number


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identify = @"reuse";
    
    UserPageCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    
    if ( cell == nil) {
        cell = [[UserPageCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identify    ];
        
    }
    
    if (0 == indexPath.row) {
        cell.imageview.image = [UIImage imageNamed:@"Favorites.png"];
        cell.titleLabel.text = @"我的收藏";
        
    }else if (1 == indexPath.row){
        
        cell.imageview.image = [UIImage imageNamed:@"ClearCache.png"];
        cell.titleLabel.text = @"清除缓存";
        
    }else if (2 == indexPath.row){
        
        cell.imageview.image = [UIImage imageNamed:@"aboutUs.png"];
        cell.titleLabel.text = @"关于我们";
    }
    
    
    return  cell;
    
} //cell


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        MineResaveViewController *mineResave = [[MineResaveViewController alloc]init];
        [self.navigationController pushViewController:mineResave animated:YES];
        
        
    }
    if (indexPath.row == 2) {
        
        AboutUsViewController *aboutUsVC = [[AboutUsViewController alloc] init];
        [self.navigationController pushViewController:aboutUsVC animated:YES];
        
    }

    
    
}//选中触发方法


-(void)tapAction:(UITapGestureRecognizer *)sender{
    
    NSLog(@"tap");
    if (self.loginState == NO) {
        
        //微博登陆
        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
        
        [UMSocialData setAppKey:@"507fcab25270157b37000010"];
        
        snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
            
            //          获取微博用户名、uid、token等
            
            if (response.responseCode == UMSResponseCodeSuccess) {
                
                UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
                
                NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
                
                NSURL *userUrl = [NSURL URLWithString:snsAccount.iconURL];  //转换 url
                [self.userImageView sd_setImageWithURL:userUrl];   //修改头像
                [self.loginButton setTitle:snsAccount.userName forState:(UIControlStateNormal)];  //修改用户名
                
                //增加提示
                UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提  示" message:@"点击头像可以退出登陆" delegate: self cancelButtonTitle:@"朕知道了..." otherButtonTitles: nil];
                
                alert1.tag = 1001;
                
                
                [alert1 show];
                
                //修改登陆状态
                self.loginState = YES;
                
            }});//block
        
        
    }else if (self.loginState == YES){
        
        NSLog(@"退出登陆");
        
        UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"提  示" message:@"确定退出登陆?" delegate: self cancelButtonTitle:nil otherButtonTitles:@"残忍退出", nil];
        
        alert2.tag = 1002;
        
        [alert2 show];
        
        
        //修改登陆状态
        self.loginState = NO;
        
    }
    
    

}//头像轻拍手势

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (1002 == alertView.tag && 0 == buttonIndex) {
        NSLog(@"1002");
        
        //取消登陆
        [[UMSocialDataService defaultDataService] requestUnOauthWithType:UMShareToSina  completion:^(UMSocialResponseEntity *response){
            NSLog(@"response is %@",response);
        }];
        
        self.userImageView.image = [UIImage imageNamed:@"userHead.png"];
        [self.loginButton setTitle:@"点击头像登陆" forState:(UIControlStateNormal)];
        
    }
    
    
} //alertview 点击方法


@end













